package com.mr.cai.main.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.mr.cai.main.R;
import com.mr.cai.main.activity.base.MrCaiBaseActivity;
import com.mr.cai.main.bean.MrCaiPublicStatusBean;
import com.mr.cai.main.httpApi.MrCaiHttpApi;
import com.mr.cai.main.parse.MrCaiPublicStatusBeanParse;
import com.mr.cai.main.utils.CommonCoreUtil;
import com.mr.cai.main.view.LineEditText;

import rx.Observable;
import rx.Subscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.functions.Action1;

/**
 * Created by hai8108 on 16/2/9.
 */
public class ResetPwdActivity extends MrCaiBaseActivity{
    private ProgressBar pb_loaing;
    private LineEditText pwdEdt;
    private TextView pwdResetNext;
    private Subscription registerSubscription;
    private String mobileNum;
    public static void launchActivity (Activity activity, String mobileNum) {
        Intent intent = new Intent(activity, ResetPwdActivity.class);
        intent.putExtra("mobileNum", mobileNum);
        activity.startActivity(intent);
    }
    @Override
    public void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reset_pwd_layout);
        mobileNum = getIntent().getStringExtra("mobileNum");
        if(TextUtils.isEmpty(mobileNum)){
            finish();
        }
        ((TextView)findViewById(R.id.top_view).findViewById(R.id.pub_top_mid)).setText(R.string
                .reset_pwd_title);
        CommonCoreUtil.setClickAction(findViewById(R.id.top_view).findViewById(R.id.pub_top_left)
                , new Action1() {
            @Override
            public void call (Object o) {
                finish();
            }
        });
        findView();
        setAction();
    }
    private void findView(){
        pb_loaing = (ProgressBar) findViewById(R.id.pb_loaing);
        pwdEdt = (LineEditText) findViewById(R.id.reset_pwd);
        pwdResetNext = (TextView) findViewById(R.id.reset_pwd_next);
    }
    private void setAction(){
        CommonCoreUtil.setClickAction(pwdResetNext, new Action1() {
            @Override
            public void call (Object o) {
                String pwdStr = pwdEdt.getEditableText().toString();
                if (!TextUtils.isEmpty(pwdStr) && pwdStr.length() >= 6) {
                        requestForgetPwd(mobileNum, pwdStr);
                }
            }
        });
    }
    private void requestForgetPwd (String mobileNum, String pwd) {
        Observable<MrCaiPublicStatusBean> authCodeObservable = MrCaiHttpApi.requestResetPwd
                (mobileNum, pwd, new MrCaiPublicStatusBeanParse());
        registerSubscription = authCodeObservable.observeOn(AndroidSchedulers.mainThread())
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call () {
                        pb_loaing.setVisibility(View.VISIBLE);
                    }
                })
                .subscribe(new Subscriber<MrCaiPublicStatusBean>() {
                    @Override
                    public void onCompleted () {
                        pb_loaing.setVisibility(View.GONE);
                    }
                    @Override
                    public void onError (Throwable e) {
                        pb_loaing.setVisibility(View.GONE);
                        CommonCoreUtil.showToast(R.string.data_request_fail);
                    }
                    @Override
                    public void onNext (MrCaiPublicStatusBean mMrCaiPublicStatusBean) {
                        if (mMrCaiPublicStatusBean != null && mMrCaiPublicStatusBean.isResult()
                                && mMrCaiPublicStatusBean.getCode() == 1) {
                            CommonCoreUtil.showToast(R.string.pwd_set_success);
                            finish();
                        } else {
                            if (mMrCaiPublicStatusBean != null) {
                                CommonCoreUtil.showToast(mMrCaiPublicStatusBean.getMsg());
                            } else {
                                CommonCoreUtil.showToast(R.string.public_loading_net_err_txt);
                            }
                        }
                    }
                });
    }
    @Override
    protected void onDestroy () {
        super.onDestroy();
        CommonCoreUtil.hideSoftInput(pwdEdt);
        if (registerSubscription != null) {
            registerSubscription.unsubscribe();
            registerSubscription = null;
        }
    }

}
